package com.tdtxdcxm.telmanagesys.provider;

import com.tdtxdcxm.telmanagesys.ResourceTable;
import com.tdtxdcxm.telmanagesys.itemdata.PeopleItem;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class PeopleItemProvider extends BaseItemProvider {

    private List<PeopleItem> list;
    private AbilitySlice slice;

    public PeopleItemProvider(List<PeopleItem> list, AbilitySlice slice) {
        this.list = list;
        this.slice = slice;
    }

    @Override
    public int getCount() {
        return list != null?list.size():0;
    }

    @Override
    public Object getItem(int i) {
        if(list == null || (i < 0 || i >= list.size())){
            return null;
        }
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {

        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cmpt;
        if(component == null){
            cmpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_ability_listcontainer_itemlayout,null,false);
        }
        else{
            cmpt = component;
        }

        PeopleItem peopleItem = list.get(i);
        Text text = (Text) cmpt.findComponentById(ResourceTable.Id_item_text);
        text.setText(peopleItem.getName());

        return cmpt;
    }
}
